package repository

import (
	"shop/src/model"
	"xorm.io/xorm"
)

type CheckpointRepository struct {
	engine *xorm.Engine
}

func NewCheckpointRepository(engine *xorm.Engine) *CheckpointRepository {
	return &CheckpointRepository{engine: engine}
}

func (r *CheckpointRepository) GetByStoreId(storeId int64, orderType int) (*model.Checkpoint, error) {
	var checkpoint model.Checkpoint
	_, err := r.engine.
		Where("store_id = ? and order_type = ?", storeId, orderType).
		Get(&checkpoint)
	if err != nil {
		return nil, err
	}
	return &checkpoint, err
}

func (r *CheckpointRepository) Update(storeId int64, orderType int) error {
	var checkpoint model.Checkpoint
	_, err := r.engine.
		Where("store_id = ? and order_type = ?", orderType, storeId).
		Update(&checkpoint)
	if err != nil {
		return err
	}
	return nil
}
